<?php
load_theme_textdomain('verygoogle');

if ( function_exists('register_sidebar') )
{
    register_sidebar(array(
        'before_widget' => '<li id="%1$s" class="widget %2$s"><div class="rd_ul"><div class="rd_ur"><div class="rd_ll"><div class="rd_lr"><div class="in_sb">',
        'after_widget' => '</div></div></div></div></div></li>',
        'before_title' => '<h4 class="widgettitle">',
        'after_title' => '</h4>',
    ));
}

function wp_time_since ($offset=0) {
	
	// Parameters
	$t_second = __(" second",'verygoogle');
	$t_seconds = __(" seconds",'verygoogle');
	$t_minute = __(" minute",'verygoogle');
	$t_minutes = __(" minutes",'verygoogle');
	$t_hour = __(" hour",'verygoogle');
	$t_hours = __(" hours",'verygoogle');
	$t_day = __(" day",'verygoogle');
	$t_days = __(" days",'verygoogle');
	$t_month = __(" month",'verygoogle');
	$t_months = __(" months",'verygoogle');
	$t_year = __(" year",'verygoogle');
	$t_years = __(" years",'verygoogle');
	$t_separator = __(", ",'verygoogle');
	$t_ending = __(" ago",'verygoogle');
	
	$ct_time = time() + ($offset * 3600);
	
	$diff = $ct_time - get_the_time('U');

	if ($diff < 60) {
	
		if ($diff == 0 or $diff > 1) {
			return $diff . $t_seconds . $t_ending;
		} else {
			return $diff . $t_second . $t_ending;
		}
	
	} else if ($diff < 3600) {
	
		$minutes = $diff / 60;
		$minutes = explode(".", $minutes);
		$minutes = $minutes[0];
		$seconds = $diff % 60;
		
		if ($minutes == 1) {
		
			if ($seconds == 1) {
				return $minutes . $t_minute . $t_separator . $seconds . $t_second . $t_ending;
			} else if ($seconds > 1 or $seconds == 0) {
				return $minutes . $t_minute . $t_separator . $seconds . $t_seconds . $t_ending;
			}
		
		} else if ($minutes > 1) {
		
			if ($seconds == 1) {
			return $minutes . $t_minutes . $t_separator . $seconds . $t_second . $t_ending;
			} else if ($seconds > 1 or $seconds == 0) {
			return $minutes . $t_minutes . $t_separator . $seconds . $t_seconds . $t_ending;
			}
		
		}
	
	} else if ($diff < 86400) {
	
		$hours = $diff / 3600;
		$hours = explode(".", $hours);
		$hours = $hours[0];
		
		$minutes = ($diff % 3600) / 60;
		$minutes = explode(".", $minutes);
		$minutes = $minutes[0];
		
		if ($hours == 1) {
		
			if ($minutes == 1) {
				return $hours . $t_hour . $t_separator . $minutes . $t_minute . $t_ending;
			} else if ($minutes > 1 or $minutes == 0) {
				return $hours . $t_hour . $t_separator . $minutes . $t_minutes . $t_ending;
			}
		
		} else if ($hours > 1) {
		
			if ($minutes == 1) {
				return $hours . $t_hours . $t_separator . $minutes . $t_minute . $t_ending;
			} else if ($minutes > 1 or $minutes == 0) {
				return $hours . $t_hours . $t_separator . $minutes . $t_minutes . $t_ending;
			}
		
		}
	
	} else if ($diff < 604800) {
	
		$days = $diff / 86400;
		$days = explode(".", $days);
		$days = $days[0];
		
		$hours = ($diff % 86400) / 3600;
		$hours = explode(".", $hours);
		$hours = $hours[0];
		
		if ($days == 1) {
		
			if ($hours == 1) {
			return $days . $t_day . $t_separator . $hours . $t_hour . $t_ending;
			} else if ($hours > 1 or $hours == 0) {
			return $days . $t_day . $t_separator . $hours . $t_hours . $t_ending;
			}
		
		} else if ($days > 1) {
		
			if ($hours == 1) {
			return $days . $t_days . $t_separator . $hours . $t_hour . $t_ending;
			} else if ($hours > 1 or $hours == 0) {
			return $days . $t_days . $t_separator . $hours . $t_hours . $t_ending;
			}
		
		}
	
	} else if ($diff < 2592000) {
	
		$days = $diff / 86400;
		$days = explode(".", $days);
		$days = $days[0];
		
		if ($hours == 1) {
			return __("$days day ago.",'verygoogle');
		} else if ($days > 1) {
			return __("$days days ago.",'verygoogle');
		}
	
	} else if ($diff < 31536000) {
	
		$months = $diff / 2592000;
		$months = explode(".", $months);
		$months = $months[0];
		
		$days = ($diff % 2592000) / 86400;
		$days = explode(".", $days);
		$days = $days[0];
		
		if ($months == 1) {
		
			if ($days == 1) {
				return $months . $t_month . $t_separator . $days . $t_day . $t_ending;
			} else if ($days > 1 or $days == 0) {
				return $months . $t_month . $t_separator . $days . $t_days . $t_ending;
			}
		
		} else if ($months > 1) {
		
			if ($days == 1) {
				return $months . $t_months . $t_separator . $days . $t_day . $t_ending;
			} else if ($days > 1 or $days == 0) {
				return $months . $t_months . $t_separator . $days . $t_days . $t_ending;
			}
		
		}
	
	} else if ($diff >= 31536000) {
	
		$years = $diff / 31536000;
		$years = explode(".", $years);
		$years = $years[0];
		
		$months = ($diff % 31536000) / 2592000;
		$months = explode(".", $months);
		$months = $months[0];
		
		if ($years == 1) {
		
			if ($months == 1) {
				return $years . $t_year . $t_separator . $months . $t_month . $t_ending;
			} else if ($months > 1 or $months == 0) {
				return $years . $t_year . $t_separator . $months . $t_months . $t_ending;
			}
		
		} else if ($years > 1) {
		
			if ($months == 1) {
				return $years . $t_years . $t_separator . $months . $t_month . $t_ending;
			} else if ($months > 1 or $months == 0) {
				return $years . $t_years . $t_separator . $months . $t_months . $t_ending;
			}
			
		}
	
	}
}

if(function_exists('get_timespan_most_viewed')){
function my_get_timespan_most_viewed($mode='', $limit=10, $days=7, $display=true, $with_bot=true, $chars=0) {
	global $wpdb, $post;
	$limit_date = current_time('timestamp') - ($days*86400);
	$limit_date = date('Y-m-d H:i:s', $limit_date);
	$output_format = get_settings('PV+_option');
	if( $mode=='post' ) {
		$where = 'p.post_type = "post"';
	} elseif( $mode=='page' ) {
		$where = 'p.post_type = "page"';
	} else {
		$where = '(p.post_type = "post" OR p.post_type = "page")';
	}
	if( $with_bot ) {
		$most_views = $wpdb->get_results('SELECT p.ID, p.post_title, p.post_name, (CAST(pm1.meta_value AS UNSIGNED) + CAST(pm2.meta_value AS UNSIGNED)) AS views FROM '.$wpdb->posts.' AS p LEFT JOIN '.$wpdb->postmeta.' AS pm1 ON pm1.post_id = p.ID AND pm1.meta_key = "views" LEFT JOIN '.$wpdb->postmeta.' AS pm2 ON pm2.post_id = p.ID AND pm2.meta_key = "bot_views" WHERE p.post_date < "'.current_time('mysql').'" AND p.post_date > "'.$limit_date.'" AND p.post_status = "publish" AND '.$where.' AND p.post_password = "" ORDER BY views DESC LIMIT '.$limit);
		$output_format = $output_format['mostviewsbot'];
	} else {
		$most_views = $wpdb->get_results('SELECT p.ID, p.post_title, p.post_name, CAST(pm.meta_value AS UNSIGNED) AS views FROM '.$wpdb->posts.' AS p LEFT JOIN '.$wpdb->postmeta.' AS pm ON pm.post_id = p.ID AND pm.meta_key = "views" WHERE p.post_date < "'.current_time('mysql').'" AND p.post_date > "'.$limit_date.'" AND p.post_status = "publish" AND '.$where.' AND p.post_password = "" ORDER BY views DESC LIMIT '.$limit);
		$output_format = $output_format['mostviewsnobot'];
	}
	if( $most_views ) {
		$output = '';
		foreach($most_views as $most_view) {
			$post_title = $most_view->post_title;
			$post_views = number_format(intval($most_view->views));
			$permalink = '/' . $most_view->post_name . '.htm';
			//$link = '<a href="'.get_permalink($most_view->ID).'">'.snippet_chars($post_title, $chars).'</a>';
			$link = '<a href="'.$permalink .'">'.snippet_chars($post_title, $chars).'</a>';
			$output .= '<li>'.sprintf($output_format,$post_views,$link).'</li>'."\n";
		}
	} else {
		$output = '<li>'.__('N/A', 'postviews_plus').'</li>'."\n";
	}
	if( $display ) {
		echo $output;
	} else {
		return $output;
	}
}

function my_get_most_viewed($mode='', $limit=10, $chars=0, $display=true, $with_bot=true) {
	global $wpdb, $post;
	$output_format = get_settings('PV+_option');
	if( $mode=='post' ) {
		$where = 'p.post_type = "post"';
	} elseif( $mode=='page' ) {
		$where = 'p.post_type = "page"';
	} else {
		$where = '(p.post_type = "post" OR p.post_type = "page")';
	}
	if( $with_bot ) {
		$most_views = $wpdb->get_results('SELECT p.ID, p.post_title, p.post_name, (CAST(pm1.meta_value AS UNSIGNED) + CAST(pm2.meta_value AS UNSIGNED)) AS views FROM '.$wpdb->posts.' AS p LEFT JOIN '.$wpdb->postmeta.' AS pm1 ON pm1.post_id = p.ID AND pm1.meta_key = "views" LEFT JOIN '.$wpdb->postmeta.' AS pm2 ON pm2.post_id = p.ID AND pm2.meta_key = "bot_views" WHERE p.post_date < "'.current_time('mysql').'" AND p.post_status = "publish" AND '.$where.' AND p.post_password = "" ORDER BY views DESC LIMIT '.$limit);
		$output_format = $output_format['mostviewsbot'];
	} else {
		$most_views = $wpdb->get_results('SELECT p.ID, p.post_title, p.post_name, CAST(pm.meta_value AS UNSIGNED) AS views FROM '.$wpdb->posts.' AS p LEFT JOIN '.$wpdb->postmeta.' AS pm ON pm.post_id = p.ID AND pm.meta_key = "views" WHERE p.post_date < "'.current_time('mysql').'" AND p.post_status = "publish" AND '.$where.' AND p.post_password = "" ORDER BY views DESC LIMIT '.$limit);
		$output_format = $output_format['mostviewsnobot'];
	}
	if( $most_views ) {
		$output = '';
		foreach($most_views as $most_view) {
			$post_title = $most_view->post_title;
			$post_views = number_format(intval($most_view->views));
			$permalink = '/' . $most_view->post_name . '.htm';
			//$link = '<a href="'.get_permalink($most_view->ID).'">'.snippet_chars($post_title, $chars).'</a>';
			$link = '<a href="'. $permalink .'">'.snippet_chars($post_title, $chars).'</a>';
			$output .= '<li>'.sprintf($output_format,$post_views,$link).'</li>'."\n";
		}
	} else {
		$output = '<li>'.__('N/A', 'wp-postviews_plus').'</li>';
	}
	if( $display ) {
		echo $output;
	} else {
		return $output;
	}
}

}


?>
